<?php
// A simple Class Plurk API (using http://www.plurk.com/API)
// Version: 0.5a - 2009/12/08 
// by YoHang (yogahanggara@gmail.com - http://yohang.web.id)
// Get Plurk API Key = http://www.plurk.com/API#key

class PlurkAPI {
	var	$host = 'http://www.plurk.com/API';
	public $api_key;

	function ShowError($response) {
		$obj = json_decode($response);
		print "<strong>Plurk API Error: </strong>";
		print $obj->{'error_text'};
		print "<br />";		
	}
	
	function Execute() {
		$response = curl_exec($this->handle);
		$resultArray = curl_getinfo($this->handle);
		if($resultArray['http_code'] == "200"){
			return $response;
		} else {
			if($response != '') {
				$this->ShowError($response);
			} else {
				print "<strong>CURL Error: </strong>";
				print curl_error($this->handle);
			}
			exit();			
			//return false;
		}	
	}
	
	function ClosePlurk() {
		curl_close($this->handle); 
	}
	
	function InitPlurk($api_key) {
		$this->api_key = $api_key;	
		$this->handle = curl_init();
		curl_setopt($this->handle, CURLOPT_FRESH_CONNECT, true);		
		curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->handle, CURLOPT_TIMEOUT, 60);
		//set_time_limit(60);		
	}
	
	function LoginPlurk($username, $password) {
		$ckfile = tempnam ("/tmp", "CURLCOOKIE");		
		curl_setopt($this->handle, CURLOPT_COOKIEJAR, $ckfile);
		curl_setopt($this->handle, CURLOPT_COOKIEFILE, $ckfile);		
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Users/login?api_key='.$this->api_key.'&username='.$username.'&password='.$password);
		$this->Execute();
	}
	
	function PostPlurk($message, $qualifier='says', $qualifier_translated='', $language='en') {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Timeline/plurkAdd?api_key='.$this->api_key.'&content='.urlencode(stripslashes(urldecode($message))).'&qualifier='.$qualifier.'&lang='.$language.'&qualifier_translated='.$qualifier_translated);		
		$this->Execute();
	}
	
	function PostResponse($plurk_id, $message, $qualifier='says') {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Responses/responseAdd?api_key='.$this->api_key.'&plurk_id='.$plurk_id.'&content='.urlencode(stripslashes(urldecode($message))).'&qualifier='.$qualifier);		
		$this->Execute();
	}	
	
	function GetOwnProfile() {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Profile/getOwnProfile?api_key='.$this->api_key);		
		$response = $this->Execute();
		$data = json_decode($response, true);
		//print "<pre>";
		//print_r($data);	
		//print "</pre>";		
		return $data;
	}
	
	function GetPublicProfile($user_id) {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Profile/getPublicProfile?api_key='.$this->api_key.'&user_id='.$user_id);		
		$response = $this->Execute();
		$data = json_decode($response, true);
		return $data;	
	}
	
	function GetPlurk($plurk_id) {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Timeline/getPlurk?api_key='.$this->api_key.'&plurk_id='.$plurk_id);		
		$response = $this->Execute();
		$data = json_decode($response, true);
		return $data;			
	}
	
	function GetPlurks($only_responded=false, $offset='', $only_user=0, $limit=20) {
		//if($only_user > 0)
		//	$only_user = '&only_user=3206593';
		if($only_responded == true)
			$only_responded = '&only_responded=true';
		if($offset != '')
			$offset = '&offset='.$offset;
						
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Timeline/getPlurks?api_key='.$this->api_key.'&limit='. $limit . $only_responded . $offset);		
		$response = $this->Execute();
		$data = json_decode($response, true);
		return $data;
	}
	
	function GetUnreadPlurks($limit=20) {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Timeline/getUnreadPlurks?api_key='.$this->api_key.'&limit='.$limit);		
		$response = $this->Execute();
		$data = json_decode($response, true);
		return $data;
	}	
	
	function GetResponses($plurk_id) {
		curl_setopt($this->handle, CURLOPT_URL, $this->host.'/Responses/get?api_key='.$this->api_key.'&plurk_id='.$plurk_id.'&from_response=0');		
		$response = $this->Execute();
		$data = json_decode($response, true);
		return $data;
	}
	
	function GetAvatar($user_id, $avatar, $size='medium') {
		$ext = '.gif';
		if($size == 'big') $ext = '.jpg';
		if($avatar == NULL)
			$avatar = '';	
		$img_path = 'http://avatars.plurk.com/'.$user_id.'-'.$size.$avatar.$ext;
		return $img_path;
	}	
}
?>